debian
set -e
set -u

# This script is to package the Cura package for Windows/Linux and Mac OS X
# This script should run under Linux and Mac OS X, as well as Windows with Cygwin.

#############################
# CONFIGURATION
#############################

##Select the build target
#BUILD_TARGET=${1:-none}
#BUILD_TARGET=win32
#BUILD_TARGET=darwin
#BUILD_TARGET=debian_i386
#BUILD_TARGET=debian_amd64
BUILD_TARGET=debian_armhf
#BUILD_TARGET=freebsd

##Do we need to create the final archive
ARCHIVE_FOR_DISTRIBUTION=1
##Which version name are we appending to the final archive
export BUILD_NAME=15.04
TARGET_DIR=Cura-${BUILD_NAME}-${BUILD_TARGET}

##Which versions of external programs to use
WIN_PORTABLE_PY_VERSION=2.7.2.1

##Which CuraEngine to use
if [ -z ${CURA_ENGINE_REPO:-} ] ; then
	CURA_ENGINE_REPO="https://github.com/Ultimaker/CuraEngine.git"
fi
if [ -z ${CURA_ENGINE_REPO_PUSHURL:-} ] ; then
	CURA_ENGINE_REPO_PUSHURL="git@github.com:Ultimaker/CuraEngine.git"
fi

#############################
# Support functions
#############################
function checkTool
{
	if [ -z "`which $1`" ]; then
		echo "The $1 command must be somewhere in your \$PATH."
		echo "Fix your \$PATH or install $2"
		exit 1
	fi
}

function downloadURL
{
	filename=`basename "$1"`
	echo "Checking for $filename"
	if [ ! -f "$filename" ]; then
		echo "Downloading $1"
		curl -L -O "$1"
		if [ $? != 0 ]; then
			echo "Failed to download $1"
			exit 1
		fi
	fi
}

function extract
{
	echo "Extracting $*"
	echo "7z x -y $*" >> log.txt
	7z x -y $* >> log.txt
	if [ $? != 0 ]; then
        echo "Failed to extract $*"
        exit 1
	fi
}

function gitClone
{
	echo "Cloning $1 into $3"
	echo "  with push URL $2"
	if [ -d $3 ]; then
		cd $3
		git clean -dfx
		git reset --hard
		git pull
		cd -
	else
		git clone $1 $3
		git config remote.origin.pushurl "$2"
	fi
}

#############################
# Actual build script
#############################
if [ "$BUILD_TARGET" = "none" ]; then
	echo "You need to specify a build target with:"
	echo "$0 win32"
	echo "$0 debian_i386"
	echo "$0 debian_amd64"
	echo "$0 debian_armhf"
	echo "$0 darwin"
	echo "$0 freebsd"
	echo "$0 fedora                         # current   system"
	echo "$0 fedora \"mock_config_file\" ...  # different system(s)"
	exit 0
fi

if [ -z `which make` ]; then
	MAKE=mingw32-make
else
	MAKE=make
fi

# Change working directory to the directory the script is in
# http://stackoverflow.com/a/246128
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"

checkTool git "git: http://git-scm.com/"
checkTool curl "curl: http://curl.haxx.se/"
if [ $BUILD_TARGET = "win32" ]; then
	checkTool avr-gcc "avr-gcc: http://winavr.sourceforge.net/ "
	#Check if we have 7zip, needed to extract and packup a bunch of packages for windows.
	checkTool 7z "7zip: http://www.7-zip.org/"
	checkTool $MAKE "mingw: http://www.mingw.org/"
fi
#For building under MacOS we need gnutar instead of tar
if [ -z `which gnutar` ]; then
	TAR=tar
else
	TAR=gnutar
fi

#############################
# Build the required firmwares
#############################

if [ -d "C:/arduino-1.0.3" ]; then
	ARDUINO_PATH=C:/arduino-1.0.3
	ARDUINO_VERSION=103
elif [ -d "/Applications/Arduino.app/Contents/Resources/Java" ]; then
	ARDUINO_PATH=/Applications/Arduino.app/Contents/Resources/Java
	ARDUINO_VERSION=105
else
	ARDUINO_PATH=/usr/share/arduino
	ARDUINO_VERSION=105
fi

if [ ! -d "$ARDUINO_PATH" ]; then
  echo "Arduino path '$ARDUINO_PATH' doesn't exist"
  #exit 1
fi




#############################
# Debian armhf .deb
#############################

if [ "$BUILD_TARGET" = "debian_armhf" ]; then
	export CXX="g++"
	gitClone \
          ${CURA_ENGINE_REPO} \
          ${CURA_ENGINE_REPO_PUSHURL} \
          CuraEngine
	if [ $? != 0 ]; then echo "Failed to clone CuraEngine"; exit 1; fi
        $MAKE -C CuraEngine VERSION=${BUILD_NAME}
	if [ $? != 0 ]; then echo "Failed to build CuraEngine"; exit 1; fi
	sudo chown `whoami`:`whoami` scripts/linux/${BUILD_TARGET} -R
	rm -rf scripts/linux/${BUILD_TARGET}/usr/share/cura
	mkdir -p scripts/linux/${BUILD_TARGET}/usr/share/cura
	cp -a Cura scripts/linux/${BUILD_TARGET}/usr/share/cura/
	cp -a resources scripts/linux/${BUILD_TARGET}/usr/share/cura/
	cp -a plugins scripts/linux/${BUILD_TARGET}/usr/share/cura/
	mkdir -p scripts/linux/${BUILD_TARGET}/usr/bin
	cp -a CuraEngine/build/CuraEngine scripts/linux/${BUILD_TARGET}/usr/bin/
	cp scripts/linux/cura.py scripts/linux/${BUILD_TARGET}/usr/share/cura/
	echo $BUILD_NAME > scripts/linux/${BUILD_TARGET}/usr/share/cura/Cura/version
	sudo chown root:root scripts/linux/${BUILD_TARGET} -R
	sudo chmod 755 scripts/linux/${BUILD_TARGET}/usr -R
	sudo chmod 755 scripts/linux/${BUILD_TARGET}/DEBIAN -R
	cd scripts/linux
	dpkg-deb --build ${BUILD_TARGET} $(dirname ${TARGET_DIR})/cura_${BUILD_NAME}-${BUILD_TARGET}.deb
	sudo chown `id -un`:`id -gn` ${BUILD_TARGET} -R
	exit
fi

#############################
# Build the packages
#############################
rm -rf ${TARGET_DIR}
mkdir -p ${TARGET_DIR}

rm -f log.txt

#add Cura
mkdir -p ${TARGET_DIR}/Cura ${TARGET_DIR}/resources ${TARGET_DIR}/plugins
cp -a Cura/* ${TARGET_DIR}/Cura
cp -a resources/* ${TARGET_DIR}/resources
cp -a plugins/* ${TARGET_DIR}/plugins
#Add cura version file
echo $BUILD_NAME > ${TARGET_DIR}/Cura/version


echo "Installed into ${TARGET_DIR}"

